package com.jhhc.WaterQuality.BaseInfo;

import com.jhhc.FloodForecast.Sensor.Pojo.BDMS_ST_STBPRP_B;
import com.jhhc.FloodForecast.Sensor.Service.BDMS_ST_STBPRP_BService;
import com.jhhc.WaterQuality.BaseInfo.entity.BDMS_BA_BASINSCHEME_B;
import com.jhhc.WaterQuality.BaseInfo.entity.BDMS_YB_FORECASTSCHEME_B;
import com.jhhc.WaterQuality.BaseInfo.entity.BDMS_YB_FORECASTSTATION_B;
import com.jhhc.WaterQuality.BaseInfo.entity.result.BasinSchemeInfo;
import com.jhhc.WaterQuality.BaseInfo.entity.result.BoundaryStationInfo;
import com.jhhc.WaterQuality.BaseInfo.entity.result.ForecastSchemeInfo;
import com.jhhc.WaterQuality.BaseInfo.entity.result.ForecastStationInfo;
import com.jhhc.WaterQuality.BaseInfo.service.BDMS_BA_BASINSCHEME_BService;
import com.jhhc.WaterQuality.BaseInfo.service.BDMS_YB_FORECASTSCHEME_BService;
import com.jhhc.WaterQuality.BaseInfo.service.BDMS_YB_FORECASTSTATION_BService;
import com.jhhc.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(value="WaterQualityBaseInfoController",tags={"预报基础信息"})
@RequestMapping(value = "WaterQuality/BaseInfo",headers = "Accept=application/json")
@RestController("WaterQualityBaseInfoController")
public class BaseInfoController {

    @Autowired
    private BDMS_YB_FORECASTSCHEME_BService bdms_yb_forecastscheme_bService;
    @Autowired
    private BDMS_YB_FORECASTSTATION_BService bdms_yb_forecaststation_bService;
    @Autowired
    private BDMS_ST_STBPRP_BService bdms_st_stbprp_bService;
    @Autowired
    private BDMS_BA_BASINSCHEME_BService bdms_ba_basinscheme_bService;
    //GetForecastBaseInfos
    @ApiOperation("获取预报计算方案列表")
    @RequestMapping(value = "GetForecastBaseInfos", headers = "Accept=application/json", method = RequestMethod.GET)
    public JsonResult getForecastBaseInfos(@RequestParam Integer projectId) {

        //创建返回对象
        List<BasinSchemeInfo> basinSchemeInfos = new ArrayList<>();

        //根据项目id获取方案集合
        List<BDMS_BA_BASINSCHEME_B> bdms_ba_basinscheme_bs =bdms_ba_basinscheme_bService.queryById(projectId);

        for (BDMS_BA_BASINSCHEME_B bdms_ba_basinscheme_b:bdms_ba_basinscheme_bs){
            BasinSchemeInfo basinSchemeInfo = new BasinSchemeInfo();
            BoundaryStationInfo boundaryStationInfo = new BoundaryStationInfo();
            basinSchemeInfo.setBasinSchemeId(bdms_ba_basinscheme_b.getBschemeid());
            basinSchemeInfo.setBasinSchemeName(bdms_ba_basinscheme_b.getBschemenm());
            basinSchemeInfo.setBasin(bdms_ba_basinscheme_b.getBasin());
            String [] bound =bdms_ba_basinscheme_b.getUpb().split(",");
            boundaryStationInfo.setBoundaryName(bound[0]);
            boundaryStationInfo.setStationCode(bound[1]);
            basinSchemeInfo.setBoundaryStationInfo(boundaryStationInfo);
            basinSchemeInfo.setForecastReachName(bdms_ba_basinscheme_b.getRivers());
            List<ForecastSchemeInfo> forecastSchemeInfos = new ArrayList<>();
            List<BDMS_YB_FORECASTSCHEME_B> bdms_yb_forecaststation_bs = bdms_yb_forecastscheme_bService.queryById(projectId,basinSchemeInfo.getBasinSchemeId());
            for (BDMS_YB_FORECASTSCHEME_B bdms_yb_forecaststation_b:bdms_yb_forecaststation_bs){
                ForecastSchemeInfo forecastSchemeInfo = new ForecastSchemeInfo();
                //List<ForecastStationInfo> forecastStationInfos = new ArrayList<>();
                forecastSchemeInfo.setForecastSchemeId(bdms_yb_forecaststation_b.getFschemeid());
                forecastSchemeInfo.setForecastSchemeName(bdms_yb_forecaststation_b.getFschemenm());
                forecastSchemeInfo.setRiverModel(bdms_yb_forecaststation_b.getRivermodel());
                forecastSchemeInfo.setCalMaxT(bdms_yb_forecaststation_b.getCalmaxt());
                forecastSchemeInfo.setCalInterval(bdms_yb_forecaststation_b.getCalinterval());
                forecastSchemeInfo.setCalBackDay(bdms_yb_forecaststation_b.getCalbackt());
                forecastSchemeInfo.setOrder(bdms_yb_forecaststation_b.getFschemeorder());
                forecastSchemeInfo.setForecastStationInfos(getStationInfo(projectId,bdms_yb_forecaststation_b.getFschemeid()));
                forecastSchemeInfos.add(forecastSchemeInfo);
            }
            basinSchemeInfo.setForecastSchemeInfos(forecastSchemeInfos);
            basinSchemeInfos.add(basinSchemeInfo);
        }




        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(basinSchemeInfos);
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        return jsonResult;
    }

    public   List<ForecastStationInfo> getStationInfo(Integer projectId,String forecastSchemeId){
        List<ForecastStationInfo> forecastStationInfos = new ArrayList<>();
        //获取预报站点信息
        List<BDMS_YB_FORECASTSTATION_B> bdms_yb_forecaststation_bs = bdms_yb_forecaststation_bService.queryById(projectId,forecastSchemeId);
        //获取站点信息
        List<BDMS_ST_STBPRP_B> bdms_st_stbprp_bs = bdms_st_stbprp_bService.selectStationByProject(projectId);
        for (BDMS_YB_FORECASTSTATION_B bdms_yb_forecaststation_b:bdms_yb_forecaststation_bs){
            ForecastStationInfo forecastStationInfo = new ForecastStationInfo();
            forecastStationInfo.setForecastStationCode(bdms_yb_forecaststation_b.getFstcd());
            forecastStationInfo.setForecastStationName(bdms_yb_forecaststation_b.getFstnm());
            forecastStationInfo.setStationCode(bdms_yb_forecaststation_b.getObsstcd());
            forecastStationInfo.setStationName(bdms_yb_forecaststation_b.getObsstnm());
            BDMS_ST_STBPRP_B bdmsStStbprpB =   bdms_st_stbprp_bs.stream().filter(bDMSSTSTBPRPB ->bDMSSTSTBPRPB.getStcd().equals(forecastStationInfo.getStationCode())).findAny().orElse(null);
            if (bdmsStStbprpB != null) {
                forecastStationInfo.setLgtd(bdmsStStbprpB.getLgtd());
                forecastStationInfo.setLttd(bdmsStStbprpB.getLttd());
            }

            forecastStationInfo.setOrder(bdms_yb_forecaststation_b.getFstationorder());
            forecastStationInfo.setStationType(bdms_yb_forecaststation_b.getFsttp());
            forecastStationInfos.add(forecastStationInfo);
        }
        return forecastStationInfos;
    }

    //GetForecastStationInfos
    @ApiOperation("获取预报站点信息")
    @RequestMapping(value = "GetForecastStationInfos", headers = "Accept=application/json", method = RequestMethod.GET)
    public JsonResult getForecastStationInfos(@RequestParam Integer projectId,@RequestParam String forecastSchemeId) {
        //返回对象
        //List<ForecastStationInfo> forecastStationInfos = new ArrayList<>();

        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(getStationInfo(projectId,forecastSchemeId));
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        return jsonResult;
    }

}
